// pages/mine/mineData/mineData.js
var app = getApp();
import Api from '../../../api/api.js';
const api = new Api();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    genderSelectIndex:0,
    dateText:"请选择出生日期",
    cityName:"请选择所在城市",
    dataDict:{},
    headImage:"",
    nickname:"",
    sex:"",
    description:"",
    city:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  genderChooseClick(e){
    var key = e.currentTarget.dataset.key;
    this.setData({
      genderSelectIndex:key
    });
  },
  getData(){
    var _this = this;
    var map = {};
    map.appId = app.APP_ID;
    map.openId = app.OPEN_ID();
    api.getRequestData(app.getUserDataByOpenIdUrl, map, false, "GET").then(res => {
      if (res.data.errorCode == 0) {
        var data = res.data.model;
        var sex = data.sex;
        if (sex == 1) {
          _this.setData({
            genderSelectIndex:1
          });
        } else {
          _this.setData({
            genderSelectIndex: 0
          });
        }
        _this.setData({
          headImage:data.headimg,
          nickname: data.nickname,
          dateText: data.birthday,
          cityName: data.city,
          description: data.description,
          dataDict:data
        });
      } else {
        app.alert(res.data.errorMsg);
      }
    });
  },
  bindStartDateChange(e) {
    this.setData({
      dateText: e.detail.value
    });
  },
  changedHeadImageClick(){
    var _this = this;
    app.chooseImageAndUpLoad(1).then(res => {
      if (res) {
        var url = res[0];
        _this.setData({
          headImage:url
        });
      }
    });
  },
  nickNameInput(e){
    var value = e.detail.value;
    this.setData({
      nickname:value
    });
  },
  desInput(e){
    var value = e.detail.value;
    this.setData({
      description: value
    });
  },
  goToChooseCity(){
    wx.navigateTo({
      url: '/pages/release/selectCity/selectCity',
    })
  },
  saveUserData(){
    if (this.data.headImage.length == 0){
      app.alert("请上传头像~");
      return;
    }
    if (this.data.nickname.length == 0 || this.data.nicknam == "null") {
      app.alert("请输入昵称~");
      return;
    }

    if (this.data.dateText == null || this.data.dateText.length == 0 || this.data.dateText == "null" || this.data.dateText == "请选择出生日期") {
      app.alert("请选择生日~");
      return;
    }
    if (this.data.cityName.length == 0 || this.data.cityName == "null" || this.data.cityName == "请选择所在城市") {
      app.alert("请选择所在城市~");
      return;
    }
    console.log("=====",this.data.description);
    if (this.data.description.length == 0 || this.data.description == "null") {
      app.alert("请输入简介~");
      return;
    }
    var map = {};
    map.appid = app.APP_ID;
    map.birthday = this.data.dateText;
    map.city = this.data.cityName;
    map.description  = this.data.description;
    map.headimg = this.data.headImage;
    map.id = this.data.dataDict.id;
    map.nickname = this.data.nickname;
    map.sex = this.data.genderSelectIndex == 1 ? '1':'2';
    map.openId = this.data.dataDict.openId;
    map.unionid = this.data.dataDict.unionid;
    var _this = this;
    console.log("==========",JSON.stringify(map));
    api.getRequestData(app.userDataUpdateUrl, map, false, "POST").then(res => {
      if (res.data.errorCode == 0) {
        app.showToasts("资料修改成功~");
        setTimeout(function(){
          wx.navigateBack({});
        },1500);
      } else {
        app.alert(res.data.errorMsg);
      }
    });
     
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})